package com.mdp.im.core.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.im.core.vo.GroupUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mdp.core.service.BaseService;

import com.mdp.im.core.entity.ImGroupUser;
import com.mdp.im.core.mapper.ImGroupUserMapper;
import org.springframework.util.StringUtils;

/**
 * @author 唛盟开源 code-gen
 * @since 2024-8-13
 */
@Service
public class ImGroupUserService extends BaseService<ImGroupUserMapper,ImGroupUser> {
	static Logger logger =LoggerFactory.getLogger(ImGroupUserService.class);

	/**
	 * 自定义查询，支持多表关联
	 * @param page 分页条件
	 * @param ew 一定要，并且必须加@Param("ew")注解
	 * @param ext 如果xml中需要根据某些值进行特殊处理，可以通过这个进行传递，非必须，注解也可以不加
	 * @return
	 */
	public List<Map<String,Object>> selectListMapByWhere(IPage page, QueryWrapper ew, Map<String,Object> ext){
		return baseMapper.selectListMapByWhere(page,ew,ext);
	}

	@Autowired
	ImUserService imUserService;
	/**
	 * 批量插入群组成员 先判断是否已经存在，存在则忽略，不存在则插入
	 * @param groupId
	 * @param groupUsers
	 */
	public void batchInsertOrUpdate(String groupId,List<com.mdp.im.core.entity.ImGroupUser> groupUsers) {
		if(groupUsers==null || groupUsers.size()<=0) {
			return;
		}
		List<String> userids=new ArrayList<>();
		for (com.mdp.im.core.entity.ImGroupUser guser : groupUsers) {
			userids.add(guser.getUserid());
		}
		Map<String,Object> p=new HashMap<>();
		p.put("userids", userids);
		p.put("groupId", groupId);
		List<String> existsUserids=baseMapper.selectExistsUsersByGroupIdAndUserids(p);
		Map<String,Boolean> add=new HashMap<>();
		List<com.mdp.im.core.entity.ImGroupUser> notExistsGroupUsers=new ArrayList<>();
		for (com.mdp.im.core.entity.ImGroupUser guser : groupUsers) {
			if(StringUtils.isEmpty(guser.getStatus())) {
				guser.setStatus("1");
			}
			boolean exists=false;
			for (String userid : existsUserids) {
				if(userid.equals(guser.getUserid())) {
					exists=true;
					break;
				}
			}
			if(exists==false) {
				if(StringUtils.isEmpty(guser.getId())) {
					guser.setId(super.createKey("id"));
				}
				if(!add.containsKey(guser.getUserid())) {
					add.put(guser.getUserid(), true);
					notExistsGroupUsers.add(guser);
				}
			}
		}
		if(notExistsGroupUsers.size()>0) {
			super.batchInsert(notExistsGroupUsers);
		}

		if(existsUserids.size()>0) {
			Map<String,Object> p2=new HashMap<>();
			p2.put("userids", existsUserids);
			p2.put("groupId", groupId);
			baseMapper.batchJoinGroupByGroupIdAndUserids(p2);
		}
	}

	public void batchLeaveGroupByGroupIdAndUserids(String groupId, List<com.mdp.im.core.entity.ImGroupUser> groupUsers) {
		List<String> userids=new ArrayList<>();
		for (com.mdp.im.core.entity.ImGroupUser imGroupUser : groupUsers) {
			userids.add(imGroupUser.getUserid());
		}
		Map<String,Object> map=new HashMap<>();
		map.put("userids", userids);
		map.put("groupId", groupId);
		baseMapper.batchLeaveGroupByGroupIdAndUserids(map);
	}

	/** 请在此类添加自定义函数 */
	public List<GroupUserVo> listGroupUsers(String groupId) {

		List<GroupUserVo> users= baseMapper.listGroupUsers(groupId);
		return users;
	}
	/**
	 * 查询该用户所有的groupusers
	 * @param map
	 * */
	public List<ImGroupUser> listAllGroupUsers(Map map) {

		List<ImGroupUser> allGroupUsers= baseMapper.listAllGroupUsers(map);
		return allGroupUsers;
	}

	/**
	 * 查询在线的并且正在接待的客户人数最少的客服专员
	 * @param branchId
	 * @param cuserid
	 * @param groupId
	 * @param categoryId
	 */
	public List<Map<String,Object>> selectOnlineAndMinCustCssWorkers(String branchId, String cuserid, String groupId, String categoryId) {
		Map<String,Object> parameter=new HashMap<>();
		parameter.put("branchId", branchId);
		parameter.put("cuserid", cuserid);
		parameter.put("groupId", groupId);
		parameter.put("categoryId", categoryId);
		return baseMapper.selectOnlineAndMinCustCssWorkers(parameter);

	}
}

